home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1054 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.1 KB  |  72 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: Igor Boukanov <Igor.Boukanov@fi.uib.no>
  3. Newsgroups: comp.std.c++
  4. Subject: mutable can be removed...
  5. Date: 12 Apr 1996 08:53:37 PDT
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: Fri, 12 Apr 1996 12:25:01 +0200 (METDST)
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBVAwUBMW58gUy4NqrwXLNJAQERagH6A+IAvxCnueDQoUmjWiUQU7WcQX1+QIgn
  13.     4d3oCcO72PmA8wuh5TJwERWMjAhzWwu+g8tdzcS1jP5iIhhsfrURwg==
  14.     =+fjG
  15. Originator: austern@isolde.mti.sgi.com
  16.  
  17. Subject: mutable can be removed...
  18. Newsgroups: comp.std.c++
  19. Organization: Fysisk institutt, Universitetet i Bergen
  20. Summary: mutable semantic can be realized by template function.
  21. Keywords: C++, mutable
  22.  
  23. Consider the following:
  24.  
  25. template<class T> T* remove_constness_from(const T* p)
  26. {
  27.    return const_cast<T*>(p);
  28. }
  29.  
  30. So now instead of:
  31.  
  32. struct X { 
  33.    mutable int i; 
  34.    void set(int i_) const; 
  35. };
  36. void X::set(int i_) const { 
  37.    i = i_; 
  38.  
  39. one can wright:
  40.  
  41. struct X { 
  42.    int i; 
  43.    void set(int i_) const;
  44. };
  45. void X::set(int i_) const {
  46.    remove_constness_from(this)->i = i_; 
  47.  
  48. And remove_constness_from is more useful than mutable because in this 
  49. case to make any class member looks like mutable one don't even need to 
  50. modify class definition! And of cause lines like 
  51. "remove_constness_from(this)->i = i_;"
  52. will show explicitly what somebody does...
  53.  
  54. >From this point of view mutable keyword can be removed from C++. And it 
  55. will not break any real code because as I now there is no compiler that 
  56. can compile such code!
  57. And of cause this will make C++ standard shorter...
  58.  
  59. --
  60. Regards, Igor Boukanov. 
  61. mailto:igor.boukanov@fi.uib.no  
  62. http://www.fi.uib.no/~boukanov/
  63. ---
  64. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  65.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  66.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  67.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  68.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  69. ]
  70.